package base.input;
import java.awt.Window;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.HashMap;

public class InputHandler implements KeyListener
{
	private HashMap<Integer, ArrayList<IInput>> inputMap = new HashMap<Integer, ArrayList<IInput>>();
	
	public InputHandler(Window par1Window)
	{
		par1Window.addKeyListener(this);
	}
	
	/**Registers input to mapping with object*/
	public void registerKey(int par1, IInput par2IInput)
	{
		ArrayList<IInput> list = inputMap.containsKey(par1) ? inputMap.get(par1) : new ArrayList<IInput>();
		list.add(par2IInput);
		inputMap.put(par1, list);
	}
	
	public void keyPressed(KeyEvent e)
	{
		if (inputMap.containsKey(e.getKeyCode()))
		{
			ArrayList<IInput> objList = (ArrayList<IInput>) inputMap.get(e.getKeyCode());
			for(IInput obj : objList)
			{
				obj.onInputUpdate(e.getKeyCode(), true);
			}
		}
	}

	public void keyReleased(KeyEvent e)
	{
		if (inputMap.containsKey(e.getKeyCode()))
		{
			ArrayList<IInput> objList = (ArrayList<IInput>) inputMap.get(e.getKeyCode());
			for(IInput obj : objList)
			{
				obj.onInputUpdate(e.getKeyCode(), false);
			}
		}
	}

	public void keyTyped(KeyEvent e) {}
}
